# import redis.asyncio as redis
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from path import Path
# from fastapi_limiter import FastAPILimiter
# from redis.asyncio import Redis

from app.api.routes.users import router as users_router
from app.api.routes.websocket import router as websocket_router
from app.api.routes.item import router as item_router
from app.api.routes.debate import router as debate_router
from app.api.routes.community import router as community_router
from app.api.routes.training import router as training_router

from sqlmodel import SQLModel
from app.database import engine

app = FastAPI()

app.include_router(users_router, prefix="/auth/users")
app.include_router(item_router, prefix="/api")
app.include_router(debate_router, prefix="/api")
app.include_router(community_router, prefix="/api")
app.include_router(training_router, prefix="/api")


app.include_router(websocket_router, prefix="/api/chat")

# pool = redis.ConnectionPool.from_url(
#     "redis://localhost",
#     encoding='utf-8',
#     decode_responses=True
# )

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:4000"],  # 允许的前端域名
    allow_credentials=True,
    allow_methods=["*"],  # 允许的 HTTP 方法
    allow_headers=["*"],  # 允许的请求头
)

@app.on_event("startup")
def startup_event():
    # global redis_connection
    # redis_connection = redis.Redis(connection_pool=pool)
    # await FastAPILimiter.init(redis_connection)
    # 自动创建所有表
    SQLModel.metadata.create_all(engine)
